<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//详情页 控制器
class ContentController extends Controller{
//	显示页面
	public function index(){
//		p($_SERVER);
$_SESSION['history_url'] = $_SERVER['REQUEST_URI'];
//		sp($_SESSION);
//		接收get传递的gid(点击列表图片传递的)
		$gid = Q('get.gid',0,'intval');
//		1、实例化，根据gid 查询数据库
		$goodDeModel = new \Admin\Model\GoodsDetail;
		$goodsDeData = $goodDeModel->where("goods_gid={$gid}")->find();
		$imgArr = explode(',',$goodsDeData['img']);

//		循环 截取字符串
		$newImgArr = array();
		foreach($imgArr as $k => $v){
			$newImgArr[] = substr($v,21);
		}
//		sp($newImgArr);
//		分配变量(放大镜图片)
		View::with('imgArr',$newImgArr);
//		2、商品商品简单介绍
		$goodsModel = new \Admin\Model\Goods;
		$goodData = $goodsModel->where("gid={$gid}")->find();
//		sp($goodData);
//		分配变量(商品简单介绍)
		View::with('goodData',$goodData);
//		3、商品属性
		$newGoodsAttr = array();
		$goodsAttr = Db::table("goods_attr")->join("type_attr","type_attr_taid","=","taid")->where("goods_gid={$gid} AND taclass=1")->get();
		foreach($goodsAttr as $gAttr_k => $gAttr_v){
			$newGoodsAttr[$gAttr_v['type_attr_taid']][] = $gAttr_v;
		}
//		sp($newGoodsAttr);
//		分配变量(规格)
		View::with('newGoodsAttr',$newGoodsAttr);
//		4、商品详细介绍
		View::with('goodsDeData',$goodsDeData);
//		5、热销榜单(查询商品表，根据点击率排序，取出5条)
		
		$hotSell = $goodsModel->orderBy("click","DESC")->limit(5)->lists("gid,gname,shop_price,lists_pic");
//		分配变量
		foreach($hotSell as $hot_k => $hot_v){
			$newhotSell[] = $hot_v;
		}
//		sp($newhotSell);
		View::with('hotSell',$newhotSell);
//		面包屑
		$sid = Q('get.cid',0,'intval');
		
		$mianbao['category'] = Data::parentChannel(Db::table("category")->get(), $sid, $fieldPri = 'cid', $fieldPid = 'pid');
		$mianbao['category'] = array_reverse($mianbao['category']);
		$mianbao['goods'] = $goodData['gname'];
//				sp($mianbao);
		View::with('mianbao',$mianbao);
		View::make();
	}
//	接收ajax请求
	public function getAjaxAttr(){
		if(IS_AJAX){
//			商品属性
			$gaid = Q('post.gaid');
//			sp($gaid);
//			商品id
			$gid = Q('post.gid');
//			sp($gid);
			$Newgaid = substr($gaid,0,strlen($gaid)-1);
//			sp($Newgaid);
//			根据ajax传过来的gaid查询该商品的库存
			$goodListsModel = new \Admin\Model\GoodsLists;
			$data = $goodListsModel->where("combine='{$Newgaid}'")->find();
//			sp($data);
//			转成数组
			$data['combine'] = explode(',',$data['combine']);
//			如果该货品没有货
			if(!$data){
				echo json_encode(0);
			}else{
//			根据gid查询商品属性表
			$goodAttrModel = new \Admin\Model\GoodsAttr;
			$AttrData = $goodAttrModel->where("goods_gid={$gid} AND gaid IN({$Newgaid})")->get();
//			空数组
			$dataArr = array();
//			将原价和附件价格弄一起
			$oldprice = Q('post.price');
			$price[] = $oldprice;
			foreach($AttrData as $k => $v){
				$price[] = $v['add_price'];
			}
			foreach($AttrData as $k => $v){
				if(in_array($v['gaid'],$data['combine'])){
					$dataArr['artnum'] = $data['artnum'];
					$dataArr['inventory'] = $data['inventory'];
					$dataArr['newprice'] = array_sum($price);
				}
//				sp($v);
			}
//			sp($dataArr);
			echo json_encode($dataArr);
			exit;
//			sp($data);
//			sp($AttrData);				
			}			

		}
	}
//	购买数量与货存
	public function ajaxGetNum(){
		if(IS_AJAX){
			sp(Q('post.'));
		}
	}
}





 ?>